package com.ef.web.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.ConfirmMessage;
import com.ef.constant.ECallStatus;
import com.ef.constant.ELeadsEventType;
import com.ef.constant.ELeadsStatus;
import com.ef.constant.EUserTodoObject;
import com.ef.constant.EUserTodoStatus;
import com.ef.constant.EUserTodoType;
import com.ef.domain.Leads;
import com.ef.domain.LeadsClassEntity;
import com.ef.domain.LeadsEventHistory;
import com.ef.domain.School;
import com.ef.domain.User;
import com.ef.domain.UserTodoEvent;
import com.ef.service.LeadsClassEntityService;
import com.ef.service.LeadsEventHistoryService;
import com.ef.service.TodoEventService;
import com.ef.util.CalendarUtil;
import com.ef.util.CustomExample;
import com.ef.util.LeadsExample;

public class CCAction extends LeadsAction {

	private static final long serialVersionUID = 1L;

	// private LeadsEventHistoryService leadsEventHistoryService;

	private TodoEventService todoEventService;

	// private List<LeadsEventHistory> calls;

	private Long currentCallId;

	private String currentCallTelephone;

	private Date currentCallDealTime;

	private ECallStatus currentCallStatus = ECallStatus.Connected;

	private String currentCallComments;

	private Long todoEventId;

	private EUserTodoType todoEventType;

	private String todoEventComments;

	private Date todoEventDate;

	private String todoEventTime;

	private List<LeadsClassEntity> leadsClassEntityList;

	private LeadsClassEntityService leadsClassEntityService;

	public List<LeadsClassEntity> getLeadsClassEntityList() {
		return leadsClassEntityList;
	}

	public void setLeadsClassEntityList(
			List<LeadsClassEntity> leadsClassEntityList) {
		this.leadsClassEntityList = leadsClassEntityList;
	}

	public LeadsClassEntityService getLeadsClassEntityService() {
		return leadsClassEntityService;
	}

	public void setLeadsClassEntityService(
			LeadsClassEntityService leadsClassEntityService) {
		this.leadsClassEntityService = leadsClassEntityService;
	}

	@SuppressWarnings("serial")
	public String list() {
		fetchListData();
		CustomExample<Leads> example = new LeadsExample(this.getModel(),
				Leads.class) {
			public void appendCondition(Criteria criteria) {
				super.appendCondition(criteria);
				User user = getSessionUser();
				if (user.getRole() != null
						&& user.getRole().getName().equalsIgnoreCase("CC")) {
					criteria.createCriteria("cc").add(
							Restrictions.eq("id", getSessionUser().getId()));
				}else{
					criteria.add(Restrictions.isNotNull("cc"));
				}
				School s = getSessionUser().getSchool();
				if (s != null) {
					criteria.add(Restrictions.eq("school", s));
				}
				criteria.addOrder(Order.desc("createTime"));
			}
		};
		example.addBetweenDateProperty("createTime", selCreateStartTime,
				selCreateEndTime);
		example.addBetweenDateProperty("setApptTime", selSetApptStartTime,
				selSetApptEndTime);
		example.addBetweenDateProperty("showTime", selShowStartTime,
				selShowEndTime);
		example.addBetweenDateProperty("lastEventTime", selLastTouchStartTime,
				selLastTouchEndTime);
		example.addBetweenDateProperty("payTime", selPayStartTime,
				selPayEndTime);
		example.addBetweenDateProperty("ccAssignTime", selAssignCcStartTime,
				selAssignCcEndTime);
		this.listResult = leadsService.findByExample(example, this
				.getStartIndex(), this.getPageSize());
		return LIST;
	};

	private static SimpleDateFormat timeDf = new SimpleDateFormat("K:mm a",
			new Locale("en"));

	public String saveCall() {
		Leads leads = this.saveLeads();
		if (leads == null) {
			this.setResult(ConfirmMessage.MSG_SAVELEADS_ERROR);
			fetchListData();
			calls = leadsEventHistoryService.findByLeads(leads);
			leadsClassEntityList = leadsClassEntityService
					.getLeadsClassEntityListOrderByBookingTime(this.getModel(),
							20);
			return CALL;
		}
		Date todoEventDatetime = null;
		if (todoEventDate != null) { // todoEventDate 不为空处理
			Date time = null;
			if (todoEventTime != null && todoEventTime.trim().length() > 0) {
				try {
					time = timeDf.parse(todoEventTime);
				} catch (ParseException e) {
					System.out.println("TodoEventAction parse time error");
				}
			}
			if (time == null) {
				try {
					time = timeDf.parse("9:00 AM");
				} catch (ParseException e) {
					System.out.println("TodoEventAction parse time error");
				}
			}
			todoEventDatetime = CalendarUtil.getDateTime(todoEventDate, time);
		}
		if (currentCallTelephone != null
				&& currentCallTelephone.trim().length() > 0) {
			LeadsEventHistory currentCall = null;
			if (currentCallId == null) {
				currentCall = new LeadsEventHistory();
				leads.setCcCall(leads.getCcCall() + 1);
			} else {
				currentCall = leadsEventHistoryService.findById(currentCallId);
			}
			currentCall.setComments(currentCallComments);
			currentCall.setCallStatus(currentCallStatus);
			currentCall.setDealTime(new Date());
			currentCall.setEventType(ELeadsEventType.CCCall);
			currentCall.setLeads(leads);
			currentCall.setTelephone(currentCallTelephone);
			currentCall.setOperater(getSessionUser());
			currentCall.setNextCallTime(todoEventDatetime);
			leadsEventHistoryService.saveOrUpdate(currentCall);
			currentCallId = currentCall.getId();
			// save leads
			leads.setLastEventTime(new Date());
			leads.setLastEventCommons(currentCallComments);
		}
		if (todoEventDate != null) { // todoEventDate 不为空处理
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			String day = df.format(todoEventDate);
			String title = "";
			UserTodoEvent todoEvent = null;
			if (todoEventId == null) {
				todoEvent = new UserTodoEvent();
			} else {
				todoEvent = todoEventService.findById(todoEventId);
			}
			if (todoEventType != null
					&& todoEventType.equals(EUserTodoType.Call)) {
				todoEvent.setGoAction("/customer/CC/call?id=" + leads.getId());
				title = "NOTE:" + day + " " + todoEventTime + " need a  "
						+ todoEventType.toString() + " TO " + leads.getName();
			} else if (todoEventType != null
					&& todoEventType.equals(EUserTodoType.Demo)) {
				todoEvent.setGoAction("/customer/CC/edit?id=" + leads.getId());
				title = "NOTE:" + day + " " + todoEventTime + " need a  "
						+ todoEventType.toString() + " TO " + leads.getName();
			}
			todoEvent.setCreateTime(new Date());
			todoEvent.setTodoType(todoEventType);
			todoEvent.setCreateUser(getSessionUser());
			Date time = null;
			if (todoEventTime != null && todoEventTime.trim().length() > 0) {
				try {
					time = timeDf.parse(todoEventTime);
				} catch (ParseException e) {
					System.out.println("TodoEventAction parse time error");
				}
			} else {
				try {
					time = timeDf.parse("9:00 AM");
				} catch (ParseException e) {
					System.out.println("TodoEventAction parse time error");
				}
			}
			todoEvent.setReminderEndTime(CalendarUtil.getDateTime(
					todoEventDate, time));
			time.setTime(time.getTime() - 30 * 60 * 1000);
			todoEvent.setReminderStartTime(CalendarUtil.getDateTime(
					todoEventDate, time));
			todoEvent.setSelLeads(leads);
			todoEvent.setToObject(EUserTodoObject.MySelf);
			todoEvent.setStatus(EUserTodoStatus.Valid);
			todoEvent.setToUser(getSessionUser());
			todoEvent.setDetail(todoEventComments);
			todoEvent.setTitle(title);
			todoEventService.saveOrUpdate(todoEvent);
			todoEventId = todoEvent.getId();
		}
		if (todoEventDatetime != null) {
			leads.setNextEventTime(todoEventDatetime);
		}
		leadsService.saveOrUpdate(leads);
		fetchListData();
		calls = leadsEventHistoryService.findByLeads(leads);
		leadsClassEntityList = leadsClassEntityService
				.getLeadsClassEntityListOrderByBookingTime(this.getModel(), 20);
		this.setResult(ConfirmMessage.Call);
		return CALL;
	}

	public String demo() {
		Leads leads = this.saveLeads();
		if (leads == null) {
			this.setResult(ConfirmMessage.MSG_SAVELEADS_ERROR);
			fetchListData();
			calls = leadsEventHistoryService.findByLeads(leads);
			leadsClassEntityList = leadsClassEntityService
					.getLeadsClassEntityListOrderByBookingTime(this.getModel(),
							20);
			return EDIT;
		}
		if (this.currentCallComments != null
				&& currentCallComments.trim().length() > 0) {
			LeadsEventHistory currentCall = null;
			if (currentCallId == null) {
				currentCall = new LeadsEventHistory();
			} else {
				currentCall = leadsEventHistoryService.findById(currentCallId);
			}
			currentCall.setComments(currentCallComments);
			currentCall.setCallStatus(currentCallStatus);
			currentCall.setDealTime(new Date());
			currentCall.setEventType(ELeadsEventType.CCDemo);
			currentCall.setLeads(leads);
			currentCall.setTelephone("");
			currentCall.setOperater(getSessionUser());
			leadsEventHistoryService.saveOrUpdate(currentCall);
			currentCallId = currentCall.getId();
			leads.setLastEventTime(new Date());
			leads.setLastEventCommons(currentCallComments);
			leadsService.saveOrUpdate(leads);
		}
		;
		if (todoEventDate != null) { // todoEventDate 不为空处理
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			String day = df.format(todoEventDate);
			String title = "";
			UserTodoEvent todoEvent = null;
			if (todoEventId == null) {
				todoEvent = new UserTodoEvent();
			} else {
				todoEvent = todoEventService.findById(todoEventId);
			}
			todoEvent.setCreateTime(new Date());
			todoEvent.setTodoType(todoEventType);
			if (todoEventType != null
					&& todoEventType.equals(EUserTodoType.Call)) {
				todoEvent.setGoAction("/customer/CC/call?id=" + leads.getId());
				title = "NOTE:" + day + " " + todoEventTime + " need a  "
						+ todoEventType.toString() + " TO " + leads.getName();
			} else if (todoEventType != null
					&& todoEventType.equals(EUserTodoType.Demo)) {
				todoEvent.setGoAction("/customer/CC/edit?id=" + leads.getId());
				title = "NOTE:" + day + " " + todoEventTime + " need a  "
						+ todoEventType.toString() + " TO " + leads.getName();
			}
			todoEvent.setCreateUser(getSessionUser());
			Date time = null;
			if (todoEventTime != null && todoEventTime.trim().length() > 0) {
				try {
					time = timeDf.parse(todoEventTime);
				} catch (ParseException e) {
					System.out.println("TodoEventAction parse time error");
				}
			} else {
				try {
					time = timeDf.parse("9:00 AM");
				} catch (ParseException e) {
					System.out.println("TodoEventAction parse time error");
				}
			}
			todoEvent.setReminderEndTime(CalendarUtil.getDateTime(
					todoEventDate, time));
			time.setTime(time.getTime() - 30 * 60 * 1000);
			todoEvent.setReminderStartTime(CalendarUtil.getDateTime(
					todoEventDate, time));
			todoEvent.setSelLeads(leads);
			todoEvent.setToObject(EUserTodoObject.MySelf);
			todoEvent.setStatus(EUserTodoStatus.Valid);
			todoEvent.setToUser(getSessionUser());
			todoEvent.setDetail(todoEventComments);
			todoEvent.setTitle(title);
			todoEventService.saveOrUpdate(todoEvent);
			todoEventId = todoEvent.getId();
		}
		fetchListData();
		calls = leadsEventHistoryService.findByLeads(leads);
		leadsClassEntityList = leadsClassEntityService
				.getLeadsClassEntityListOrderByBookingTime(this.getModel(), 20);
		this.setResult(ConfirmMessage.Call);
		return EDIT;
	}

	public String call() {
		fetchListData();
		calls = leadsEventHistoryService.findByLeads(this.getModel());
		leadsClassEntityList = leadsClassEntityService
				.getLeadsClassEntityListOrderByBookingTime(this.getModel(), 20);
		return CALL;
	}

 	

	public TodoEventService getTodoEventService() {
		return todoEventService;
	}

	public void setTodoEventService(TodoEventService todoEventService) {
		this.todoEventService = todoEventService;
	}

	public Long getCurrentCallId() {
		return currentCallId;
	}

	public void setCurrentCallId(Long currentCallId) {
		this.currentCallId = currentCallId;
	}

	public String getCurrentCallTelephone() {
		return currentCallTelephone;
	}

	public void setCurrentCallTelephone(String currentCallTelephone) {
		this.currentCallTelephone = currentCallTelephone;
	}

	public ECallStatus getCurrentCallStatus() {
		return currentCallStatus;
	}

	public void setCurrentCallStatus(ECallStatus currentCallStatus) {
		this.currentCallStatus = currentCallStatus;
	}

	public Long getTodoEventId() {
		return todoEventId;
	}

	public void setTodoEventId(Long todoEventId) {
		this.todoEventId = todoEventId;
	}

	public EUserTodoType getTodoEventType() {
		return todoEventType;
	}

	public void setTodoEventType(EUserTodoType todoEventType) {
		this.todoEventType = todoEventType;
	}

	public String getTodoEventComments() {
		return todoEventComments;
	}

	public void setTodoEventComments(String todoEventComments) {
		this.todoEventComments = todoEventComments;
	}

	public Date getTodoEventDate() {
		return todoEventDate;
	}

	public void setTodoEventDate(Date todoEventDate) {
		this.todoEventDate = todoEventDate;
	}

	public String getCurrentCallComments() {
		return currentCallComments;
	}

	public void setCurrentCallComments(String currentCallComments) {
		this.currentCallComments = currentCallComments;
	}

	public Date getCurrentCallDealTime() {
		return currentCallDealTime;
	}

	public void setCurrentCallDealTime(Date currentCallDealTime) {
		this.currentCallDealTime = currentCallDealTime;
	}

	public String getTodoEventTime() {
		return todoEventTime;
	}

	public void setTodoEventTime(String todoEventTime) {
		this.todoEventTime = todoEventTime;
	}
}
